﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XianboSite.Models;

namespace XianboSite.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {
            List<SelectListItem> TempSelectListItem = new List<SelectListItem>();
            IList<LiveCategory> categories = DataAccess.GetLiveCategories();
            foreach (LiveCategory item in categories)
            {
                TempSelectListItem.Add(new SelectListItem()
                {
                    Selected = false,
                    Text = item.CategoryName,
                    Value = item.CategoryName
                });
            }
            ViewData["category"] = TempSelectListItem;
            ViewData["categories"] = categories;// DataAccess.GetLiveCategories();
            XianboSite.Models.User user = HttpContext.Session["current_user"] as XianboSite.Models.User;
            if (user != null)
            {
                ViewData["live"] = DataAccess.GetUserLives(user.UserID);
                ViewData["user"] = user;
                ViewData["attentions"] = DataAccess.GetAttentionDetails(user.UserID);
                return View();
            }

            return View();
        }

        public ActionResult Register()
        {
            return View();
        }


        [HttpPost]
        public ActionResult AddUser(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                //if (Request.IsAjaxRequest())
                //{
                // TODO: Add insert logic here
                string name = Request["tbUser"].ToString();
                string password = Request["tbPassword"].ToString();
                string email = Request["tbEmail"].ToString();
                string phonenumber = Request["tbPhonenumber"].ToString();
                password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5");
                User user = new User()
                {
                    UserName = name,
                    Email = email,
                    Password = password,
                    PhoneNumber = phonenumber,
                    UserID = XianboSite.Common.RndIDHelper.GetRnd(10, true, true, true, false, string.Empty)
                };
                DataAccess.CreateUser(user);
                return RedirectToAction("Index", "User");
                //}
                //else
                //{
                //    return RedirectToAction("Index");
                //}
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Login(FormCollection collection)
        {
            ViewData["categories"] = DataAccess.GetLiveCategories();
            if (Request["tbUser"] == null)
            {
                return View();
            }
            else
            {
                string name = Request["tbUser"].ToString();
                string password = Request["tbPassword"].ToString();
                System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
                //todo:验证
                XianboSite.Models.User user = DataAccess.GetUser(name, password);
                if (user == null) return View();
                HttpContext.Session["current_user"] = user;
                //ViewData["current_user"] = user;
                return Redirect("/User/Index");
            }
        }

        public ActionResult Logout()
        {
            HttpContext.Application["current_user"] = null;
            return Redirect("/Home/Index");
        }
    }
}
